// Modules to control application life and create native browser window
const {
    app,
    BrowserWindow,
    dialog,
    ipcMain
} = require('electron')

const {
    filters
} = require('./main/arduino')

const path = require('path')
let contents
let mainWindow
let portWindow
function createWindow() {
    mainWindow = new BrowserWindow({
        width: 1024,
        height: 768,
        minWidth: 800,
        minHeight: 600,
        icon: './build/logo128.png',
    })
    mainWindow.loadURL('http://127.0.0.1:8080')
    mainWindow.on('closed', function() {
        if (portWindow) {
            portWindow.destroy()
        }
        mainWindow = null
    })

    mainWindow.on('maximize', function() {
        mainWindow.webContents.send("maximize", true);
    })
}

function portassistant() {
    if (portWindow) {
        portWindow.show()
    } else {
        portWindow = new BrowserWindow({
            width: 768,
            height: 500,
            minWidth: 768,
            minHeight: 500,
            resizable: false,
            icon: './build/installerHeaderIcon.ico',
        })
        portWindow.loadFile('html/port/index.html')
        contents = portWindow.webContents
    }

    portWindow.on('closed', function() {
        contents = null
        portWindow = null
    })
}

app.on('ready', createWindow)

app.on('window-all-closed', function() {
    if (process.platform !== 'darwin') {
        app.quit()
    }
})

app.on('activate', function() {
    // On OS X it's common to re-create a window in the app when the
    // dock icon is clicked and there are no other windows open.
    if (mainWindow === null) {
        createWindow()
    }
})

// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.

ipcMain.on('asynchronous-message', function(event, arg) {
    dialog.showOpenDialog({
        title: 'open hello world',
        filters: [{
            name: 'blockly',
            extensions: ['xml']
        }],
        properties: ['openFile']
    }, function(filePath) {
        event.sender.send('asynchronous-reply', filePath);
    })
});

ipcMain.on('saveXml', async(event, arg) => {
    dialog.showSaveDialog(mainWindow, {
        title: '保存',
        defaultPath: path.join(__dirname, './sample/'),
        filters: [{
            name: 'Blockly',
            extensions: ['xml']
        }],
        securityScopedBookmarks: true
    }, async(filenames) => {
        try {
            let sta = await filters(filenames, arg.text)
            console.log(sta)
            event.sender.send('asynchronous-reply', [true, '保存成功'])
        } catch (err) {
            event.sender.send('asynchronous-reply', [false, '保存失败'])
        }
    })

})

ipcMain.on('open-file', (event, arg) => {
    dialog.showOpenDialog({
        title: '打开',
        defaultPath: path.join(__dirname, './sample/'),
        filters: [{
            name: 'Blockly',
            extensions: ['xml']
        }]
    }, (filenames) => {
        event.sender.send('file-path', filenames)
    })

})

ipcMain.on('openPortWin', (event, arg) => {
    portassistant()
})

ipcMain.on('closeport-admin', (event, arg) => {
    closeport()
})

ipcMain.on('closezip', (event, arg) => {
    closezip()
})

function closeport() {
    if (contents) {
        contents.send('closeport', 'whoooooooh!')

    }
}

const diely = function() { //delay
    return new Promise((res, rej) => {
        setTimeout(() => {
            res(true)
        }, 1500)
    })
}